/**
 * 格式化参数，去掉值为null，和undefined以及空字符串的字段
 * @param params
 * @returns {{}}
 */
export const formatParams = (params) => {
  let res = {};
  params &&
    Object.keys(params).forEach((key) => {
      if (params[key] !== null && params[key] !== undefined && params[key] !== '') {
        res[key] = params[key];
      }
    });
  return res;
};
/**
 * 判断字符串是否为空
 * @param str
 * @returns {boolean}
 */
export const isStrEmpty = (str) => {
  if (typeof str === 'number') {
    return false;
  } else {
    return !(str !== null && str !== undefined && str !== '' && str !== 'null');
  }
};
/**
 * 如果字符串为空,替换文本
 * @param str
 * @param replaceStr
 * @returns {*}
 */
export const replaceEmptyStr = (str, replaceStr) => {
  return isStrEmpty(str) ? replaceStr : str;
};
export const replaceComEmptyStr = (str) => {
  return replaceEmptyStr(str, '-');
};
export const limitStr = (str, limit) => {
  if (isStrEmpty(str)) {
    return '-';
  } else if (str.length <= limit) {
    return str;
  } else {
    return str.substr(0, limit) + '...';
  }
};
export const convertLongNumber = (value) => {
  let newValue = ['', '', ''];
  let fr = 1000;
  let num = 3;
  while (value / fr >= 1) {
    fr *= 10;
    num += 1;
  }
  if (num <= 4) {
    // 千
    newValue[1] = '千';
    newValue[0] = parseInt(value / 1000) + '';
  } else if (num <= 8) {
    // 万
    const text1 = parseInt(num - 4) / 3 > 1 ? '千万' : '万';
    const fm = '万' === text1 ? 10000 : 10000000;
    newValue[1] = text1;
    newValue[0] = parseInt(value / fm) + '';
  }
  if (value < 1000) {
    newValue[1] = '';
    newValue[0] = value + '';
  }
  return newValue.join('');
};
export const getNewline = (val) => {
  let str = new String(val);
  let bytesCount = 0;
  let s = '';
  for (let i = 0, n = str.length; i < n; i++) {
    let c = str.charCodeAt(i);
    // 统计字符串的字符长度
    if ((c >= 0x0001 && c <= 0x007e) || (0xff60 <= c && c <= 0xff9f)) {
      bytesCount += 1;
    } else {
      bytesCount += 2;
    }
    // 换行
    s += str.charAt(i);
    if (bytesCount >= 25) {
      s = s + '\n';
      // 重置
      bytesCount = 0;
    }
  }
  return s;
};
export function getUUID() {
  return 'xxxxxxxxxxxx4xxxyxxxxxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
    let r = (Math.random() * 16) | 0,
      v = c === 'x' ? r : (r & 0x3) | 0x8;
    return v.toString(16);
  });
}
